package com.dragon.english_practice_back.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dragon.english_practice_back.entity.TokenUsage;
import com.dragon.english_practice_back.enums.ServiceType;
import com.dragon.english_practice_back.service.TokenUsageService;
import com.dragon.english_practice_back.mapper.TokenUsageMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.stereotype.Service;

/**
* @author 伫倚
* @description 针对表【token_usage(token使用信息)】的数据库操作Service实现
* @createDate 2025-05-09 21:00:35
*/
@Service
@EnableAsync
@Slf4j
public class TokenUsageServiceImpl extends ServiceImpl<TokenUsageMapper, TokenUsage>
    implements TokenUsageService{

    // 插入消耗的token
    @Async
    public void insertTokenUsage(Integer totalTokens,
                                Integer promptTokens,
                                Integer completionTokens,
                                Integer forUserId,
                                String serviceType,
                                String model) {
        TokenUsage tokenUsage = new TokenUsage();
        tokenUsage.setTotalTokens(totalTokens);
        tokenUsage.setPromptTokens(promptTokens);
        tokenUsage.setCompletionTokens(completionTokens);
        tokenUsage.setForUserId(forUserId);
        tokenUsage.setServiceType(serviceType);
        tokenUsage.setModel(model);
        if (save(tokenUsage)){
            log.info("token usage recorded succeed for user: {}, model: {}, total: {}, prompt: {}, completion: {}, type: {}",
                    forUserId, model, totalTokens, promptTokens, completionTokens, serviceType);
        }
        else log.error("token usage recorded failed for user: {}, model: {}, total: {}, prompt: {}, completion: {}, type: {}",
                forUserId, model, totalTokens, promptTokens, completionTokens, serviceType);

    }

    // 插入对话消耗的token
    @Async
    public void chatServiceUsage(Integer totalTokens,
                                    Integer promptTokens,
                                    Integer completionTokens,
                                    Integer forUserId,
                                    String model){
        this.insertTokenUsage(totalTokens, promptTokens, completionTokens, forUserId, ServiceType.CHAT.getName(), model);
    }

    // 插入数据分析服务消耗的token
    @Async
    public void dataAnalysis(Integer totalTokens,
                             Integer promptTokens,
                             Integer completionTokens,
                             Integer forUserId,
                             String model){
        this.insertTokenUsage(totalTokens, promptTokens, completionTokens, forUserId, ServiceType.DATA_ANALYSIS.getName(), model);
    }

    // 插入翻译题生成服务消耗的token
    @Async
    public void translateQuestionGenerate(Integer totalTokens,
                                          Integer promptTokens,
                                          Integer completionTokens,
                                          Integer forUserId,
                                          String model){
        this.insertTokenUsage(totalTokens, promptTokens, completionTokens, forUserId, ServiceType.TRANSLATE_QUESTION_GENERATE.getName(), model);
    }


}




